<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:schedule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schedule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schedule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          action="http://localhost:8088/system/schedule/import"
        :show-file-list="false"
        :on-success="handleUploadSuccess"
        :before-upload="beforeUpload"
        >
        <el-button
          plain
          icon="el-icon-upload"
          size="mini"
          v-hasPermi="['system:schedule:import']"
        >上传</el-button>
        </el-upload>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:schedule:export']"
        >导出</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                        <el-form-item label="教工号" prop="teacherId" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.teacherId"
                                    placeholder="请输入教工号"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="姓名" prop="teacherName" class="el-form-search-item">
                            <el-input
                                    v-model="queryParams.teacherName"
                                    placeholder="请输入姓名"
                                    clearable
                                    size="mini"
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
            <el-form-item class="el-form-search-item">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" type="index" width="50" align="center">
            <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
            </template>
        </el-table-column>
      <el-table-column label="学年" align="center" prop="academicYear" />
      <el-table-column label="学期" align="center" prop="semester" />
      <el-table-column label="星期几" align="center" prop="dayOfWeek" />
      <el-table-column label="上课节次" align="center" prop="classNo" />
      <el-table-column label="起始周" align="center" prop="startWeek" />
      <el-table-column label="课程号" align="center" prop="courseId" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="教工号" align="center" prop="teacherId" />
      <el-table-column label="姓名" align="center" prop="teacherName" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="职称名称" align="center" prop="professionalTitle" />
      <el-table-column label="最高学历" align="center" prop="educationBackground" />
      <el-table-column label="教师所属学院" align="center" prop="department" />
      <el-table-column label="场地编号" align="center" prop="locationId" />
      <el-table-column label="场地名称" align="center" prop="locationName" />
      <el-table-column label="场地类别名称" align="center" prop="locationType" />
      <el-table-column label="场地上课起始周" align="center" prop="locationStartWeek" />
      <el-table-column label="场地上课节次" align="center" prop="locationClassTime" />
      <el-table-column label="校区" align="center" prop="campus" />
      <el-table-column label="教学班人数" align="center" prop="classSize" />
      <el-table-column label="教学班组成" align="center" prop="classComposition" />
      <el-table-column label="选课课号" align="center" prop="selectCourseId" />
      <el-table-column label="学分" align="center" prop="credits" />
      <el-table-column label="总学时" align="center" prop="totalClassHours" />
      <el-table-column label="开课学院" align="center" prop="departmentOffering" />
      <el-table-column label="选课人数" align="center" prop="enrollment" />
      <el-table-column label="周学时" align="center" prop="classHoursPerWeek" />
      <el-table-column label="上课时间" align="center" prop="classTime" />
      <el-table-column label="上课地点" align="center" prop="location" />
      <el-table-column label="课程性质" align="center" prop="courseNature" />
      <el-table-column label="座位数" align="center" prop="seats" />
      <el-table-column label="教学楼" align="center" prop="building" />
      <el-table-column label="教师联系电话" align="center" prop="phone" />
      <el-table-column label="专业组成" align="center" prop="major" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="${comment}" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schedule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schedule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课表管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学年" prop="academicYear">
          <el-input v-model="form.academicYear" placeholder="请输入学年" />
        </el-form-item>
        <el-form-item label="学期" prop="semester">
          <el-input v-model="form.semester" placeholder="请输入学期" />
        </el-form-item>
        <el-form-item label="星期几" prop="dayOfWeek">
          <el-input v-model="form.dayOfWeek" placeholder="请输入星期几" />
        </el-form-item>
        <el-form-item label="上课节次" prop="classNo">
          <el-input v-model="form.classNo" placeholder="请输入上课节次" />
        </el-form-item>
        <el-form-item label="起始周" prop="startWeek">
          <el-input v-model="form.startWeek" placeholder="请输入起始周" />
        </el-form-item>
        <el-form-item label="课程号" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程号" />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="教工号" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教工号" />
        </el-form-item>
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="职称名称" prop="professionalTitle">
          <el-input v-model="form.professionalTitle" placeholder="请输入职称名称" />
        </el-form-item>
        <el-form-item label="最高学历" prop="educationBackground">
          <el-input v-model="form.educationBackground" placeholder="请输入最高学历" />
        </el-form-item>
        <el-form-item label="教师所属学院" prop="department">
          <el-input v-model="form.department" placeholder="请输入教师所属学院" />
        </el-form-item>
        <el-form-item label="场地编号" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入场地编号" />
        </el-form-item>
        <el-form-item label="场地名称" prop="locationName">
          <el-input v-model="form.locationName" placeholder="请输入场地名称" />
        </el-form-item>
        <el-form-item label="场地上课起始周" prop="locationStartWeek">
          <el-input v-model="form.locationStartWeek" placeholder="请输入场地上课起始周" />
        </el-form-item>
        <el-form-item label="场地上课节次" prop="locationClassTime">
          <el-input v-model="form.locationClassTime" placeholder="请输入场地上课节次" />
        </el-form-item>
        <el-form-item label="校区" prop="campus">
          <el-input v-model="form.campus" placeholder="请输入校区" />
        </el-form-item>
        <el-form-item label="教学班人数" prop="classSize">
          <el-input v-model="form.classSize" placeholder="请输入教学班人数" />
        </el-form-item>
        <el-form-item label="教学班组成" prop="classComposition">
          <el-input v-model="form.classComposition" placeholder="请输入教学班组成" />
        </el-form-item>
        <el-form-item label="选课课号" prop="selectCourseId">
          <el-input v-model="form.selectCourseId" placeholder="请输入选课课号" />
        </el-form-item>
        <el-form-item label="学分" prop="credits">
          <el-input v-model="form.credits" placeholder="请输入学分" />
        </el-form-item>
        <el-form-item label="总学时" prop="totalClassHours">
          <el-input v-model="form.totalClassHours" placeholder="请输入总学时" />
        </el-form-item>
        <el-form-item label="开课学院" prop="departmentOffering">
          <el-input v-model="form.departmentOffering" placeholder="请输入开课学院" />
        </el-form-item>
        <el-form-item label="选课人数" prop="enrollment">
          <el-input v-model="form.enrollment" placeholder="请输入选课人数" />
        </el-form-item>
        <el-form-item label="周学时" prop="classHoursPerWeek">
          <el-input v-model="form.classHoursPerWeek" placeholder="请输入周学时" />
        </el-form-item>
        <el-form-item label="上课时间" prop="classTime">
          <el-input v-model="form.classTime" placeholder="请输入上课时间" />
        </el-form-item>
        <el-form-item label="上课地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入上课地点" />
        </el-form-item>
        <el-form-item label="课程性质" prop="courseNature">
          <el-input v-model="form.courseNature" placeholder="请输入课程性质" />
        </el-form-item>
        <el-form-item label="座位数" prop="seats">
          <el-input v-model="form.seats" placeholder="请输入座位数" />
        </el-form-item>
        <el-form-item label="教学楼" prop="building">
          <el-input v-model="form.building" placeholder="请输入教学楼" />
        </el-form-item>
        <el-form-item label="教师联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入教师联系电话" />
        </el-form-item>
        <el-form-item label="专业组成" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业组成" />
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule } from "@/api/system/schedule";
import axios from 'axios';


export default {
  name: "Schedule",
  data() {
    return {
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课表管理表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        teacherId: null,
        teacherName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleUploadSuccess(response, file, fileList) {
      // 处理上传成功后的逻辑
      if (response.code === 200) {
        // 文件上传成功
        // 在这里可以处理上传成功后的操作，例如刷新数据列表等
        console.log('上传成功', response);
      } else {
        // 文件上传失败
        console.error('上传失败', response);
      }
    },
    beforeUpload(file) {
      // 可以在这里进行文件类型、大小等的校验
      const isExcel = file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
      if (!isExcel) {
        this.$message.error('只能上传Excel文件');
        return false;
      }
      // 校验通过，允许上传
      return true;
    },
    /** 查询课表管理列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        academicYear: null,
        semester: null,
        dayOfWeek: null,
        classNo: null,
        startWeek: null,
        courseId: null,
        courseName: null,
        teacherId: null,
        teacherName: null,
        gender: null,
        professionalTitle: null,
        educationBackground: null,
        department: null,
        locationId: null,
        locationName: null,
        locationType: null,
        locationStartWeek: null,
        locationClassTime: null,
        campus: null,
        classSize: null,
        classComposition: null,
        selectCourseId: null,
        credits: null,
        totalClassHours: null,
        departmentOffering: null,
        enrollment: null,
        classHoursPerWeek: null,
        classTime: null,
        location: null,
        courseNature: null,
        seats: null,
        building: null,
        phone: null,
        major: null,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课表管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchedule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课表管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function() {
        return delSchedule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
